package com.chinasofti.nio;

import java.nio.ByteBuffer;

public class ByteBufferDemo02 {
    public static void main(String[] args) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(10);
        byteBuffer.put("abc".getBytes());

        //flip()切换读写模式
//        byteBuffer.flip();
        //get()读一个字节
//        while (byteBuffer.limit() != byteBuffer.position()){
//         System.out.println((char)byteBuffer.get());
//    }
//        for (int i = 0; i <byteBuffer.limit() ; i++) {
//            System.out.println((char)byteBuffer.get());
//       }
        //获取多个字节
//        byte [] bytes = new byte[byteBuffer.limit()];
//        byteBuffer.get(bytes);s
//        System.out.println(new String(bytes));
        //将position设置为0 可以重复读取 rewind
        //byteBuffer.remaining();
        //clear() 数据读写完毕(读>写)
        //array()将缓冲区转化为字节数组返回
        byte [] bytes = byteBuffer.array();
        System.out.println(new String(bytes));

    }
}

